package com.tankgame;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * 绘图区域
 */
public class MyPanel extends JPanel implements KeyListener, Runnable {
    Hero hero = null;
    Vector<EnemyTank> enemyTanks = new Vector<>();
    Vector<Bomb> bombs = new Vector<>();
    int enemyTankSize = 3;

    Image image1 = null;
    Image image2 = null;
    Image image3 = null;

    public MyPanel() {
        hero = new Hero(100, 100);
        //初始化
        for (int i = 0; i < enemyTankSize; i++) {
            EnemyTank enemyTank = new EnemyTank((100 * (i + 1)), 0);
            enemyTank.setDirect(2);

            //启动
            new Thread(enemyTank).start();
            Shot shot = new Shot(enemyTank.getX() + 10, enemyTank.getY() + 30, enemyTank.getDirect());
            enemyTank.shots.add(shot);
            new Thread(shot).start();
            enemyTanks.add(enemyTank);
        }

        image1 = Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/resources/bomb_1.gif"));
        image2 = Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/resources/bomb_2.gif"));
        image3 = Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/resources/bomb_3.gif"));
    }

    public void hitTank(Vector<Shot> shots, EnemyTank enemyTank) {
        switch (enemyTank.getDirect()) {
            case 0:
            case 2:
                for (int i = 0; i < shots.size(); i++) {
                    Shot s = shots.get(i);
                    if (s.isLive() && s.getX() > enemyTank.getX() && s.getX() < enemyTank.getX() + 20 && s.getY() > enemyTank.getY() && s.getY() < enemyTank.getY() + 30) {
                        shots.remove(s);
                        Bomb bomb = new Bomb(enemyTank.getX(), enemyTank.getY());
                        bombs.add(bomb);
                        enemyTanks.remove(enemyTank);
                        enemyTankSize--;
                    }
                }
                break;
            case 1:
            case 3:
                for (int i = 0; i < shots.size(); i++) {
                    Shot s = shots.get(i);
                    if (s.isLive() && s.getX() > enemyTank.getX() && s.getX() < enemyTank.getX() + 30 && s.getY() > enemyTank.getY() && s.getY() < enemyTank.getY() + 20) {
                        shots.remove(s);
                        Bomb bomb = new Bomb(enemyTank.getX(), enemyTank.getY());
                        bombs.add(bomb);
                        enemyTanks.remove(enemyTank);
                        enemyTankSize--;
                    }
                    break;
                }
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 900);

        if (!hero.shots.isEmpty()) {
//            for (int i = 0; i < hero.shots.size(); i++) {
//                Shot s = hero.shots.get(i);
//                if (s.isLive()) {
//                    drawProjectile(s.getX(), s.getY(), s.getDirect(), g);
//                }
//            }
            for (Shot s : hero.shots) {
                if(s.isLive()){
                    drawProjectile(s.getX(), s.getY(), s.getDirect(), g);
                }
            }

        }
        drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 0);

        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < bombs.size(); i++) {
            Bomb bomb = bombs.get(i);
            if (bomb.life > 6) {
                g.drawImage(image1, bomb.x, bomb.y, 60, 60, this);
            } else if (bomb.life > 3) {
                g.drawImage(image2, bomb.x, bomb.y, 60, 60, this);
            } else {
                g.drawImage(image3, bomb.x, bomb.y, 60, 60, this);
            }
            bomb.lifeDown();
            if(bomb.life == 0){
                bombs.remove(bomb);
            }
        }

        for (int i = 0; i < enemyTankSize; i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            if (enemyTank.isLive) {
                drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 1);
                for (int j = 0; j < enemyTank.shots.size(); j++) {
                    Shot shot = enemyTank.shots.get(j);
                    if (shot.isLive()) {
                        drawProjectile(shot.getX(), shot.getY(), shot.getDirect(), g);
                    } else {
                        enemyTank.shots.remove(shot);
                    }
                }
            }
        }
    }

    public void drawTank(int x, int y, Graphics g, int direct, int type) {
        switch (type) {
            case 0:
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
        }

        switch (direct) {
            case 0:
                g.fill3DRect(x, y, 5, 30, false);
                g.fill3DRect(x + 15, y, 5, 30, false);
                g.fill3DRect(x + 5, y + 5, 10, 20, false);
                g.fillOval(x + 5, y + 10, 10, 10);
                g.drawLine(x + 10, y + 15, x + 10, y);
                break;
            case 1:
                g.fill3DRect(x, y, 30, 5, false);
                g.fill3DRect(x, y + 15, 30, 5, false);
                g.fill3DRect(x + 5, y + 5, 20, 10, false);
                g.fillOval(x + 10, y + 5, 10, 10);
                g.drawLine(x + 15, y + 10, x + 30, y + 10);
                break;
            case 2:
                g.fill3DRect(x, y, 5, 30, false);
                g.fill3DRect(x + 15, y, 5, 30, false);
                g.fill3DRect(x + 5, y + 5, 10, 20, false);
                g.fillOval(x + 5, y + 10, 10, 10);
                g.drawLine(x + 10, y + 15, x + 10, y + 30);
                break;
            case 3:
                g.fill3DRect(x, y, 30, 5, false);
                g.fill3DRect(x, y + 15, 30, 5, false);
                g.fill3DRect(x + 5, y + 5, 20, 10, false);
                g.fillOval(x + 10, y + 5, 10, 10);
                g.drawLine(x + 15, y + 10, x, y + 10);
                break;
        }
    }

    public void drawProjectile(int x, int y, int direct, Graphics g) {
        g.setColor(Color.white);
        switch (direct) {
            case 0:
                g.drawLine(x, y, x, y + 5);
                break;
            case 1:
                g.drawLine(x, y, x + 5, y);
                break;
            case 2:
                g.drawLine(x, y, x, y - 5);
                break;
            case 3:
                g.drawLine(x, y, x - 5, y);
                break;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            hero.setDirect(0);
            hero.moveUp();
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            hero.setDirect(1);
            hero.moveRight();
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            hero.setDirect(2);
            hero.moveDown();
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            hero.setDirect(3);
            hero.moveLeft();
        } else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            hero.Fire();
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //判断是否击中
            if (!hero.shots.isEmpty()) {
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    hitTank(hero.shots, enemyTank);
                }
            }
            for(int i = 0; i < hero.shots.size(); i++){
                Shot s = hero.shots.get(i);
                if(!s.isLive()){
                    hero.shots.remove(s);
                }
            }
            this.repaint();
        }
    }
}
